<template>
  <el-container class="admin-container">
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <el-icon 
          class="fold-btn"
          @click="isCollapse = !isCollapse"
        >
          <Fold v-if="!isCollapse"/>
          <Expand v-else/>
        </el-icon>
        <span class="header-title">书籍分享平台-管理端</span>
      </div>
      <div class="header-right">
        <template v-if="adminStore.isLoggedIn">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <div class="user-info">
                <img 
                  :src="adminStore.adminInfo?.avatar_data"
                  class="user-avatar"
                  alt="管理员头像"
                />
                <span>{{ adminStore.nickname }}</span>
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </div>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="changePassword">修改密码</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </div>
    </el-header>

    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '180px'" class="aside-menu">
        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          router
          :collapse="isCollapse"
          :collapse-transition="false"
        >
          <el-menu-item 
            index="/admin/data-display"
          >
            <el-icon><DataAnalysis /></el-icon>
            <template #title>
              数据看板
            </template>
          </el-menu-item>
          
          <el-menu-item 
            index="/admin/book-manage"
          >
            <el-icon><Collection /></el-icon>
            <template #title>
              书籍管理
            </template>
          </el-menu-item>

          <el-menu-item 
            index="/admin/report-manage"
          >
            <el-icon><Bell /></el-icon>
            <template #title>
              举报管理
            </template>
          </el-menu-item>

          <el-sub-menu 
            index="member"
          >
            <template #title>
              <el-icon><UserFilled /></el-icon>
              <span>成员管理</span>
            </template>
            <el-menu-item 
              index="/admin/user-manage"
            >
              <el-icon><User /></el-icon>
              <template #title>
                用户管理
              </template>
            </el-menu-item>
            <el-menu-item 
              index="/admin/admin-manage"
            >
              <el-icon><Operation /></el-icon>
              <template #title>
                管理员管理
              </template>
            </el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="profile">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>个人中心</span>
            </template>
            <el-menu-item index="/admin/admin-profile">
              <el-icon><Avatar /></el-icon>
              <template #title>基本信息</template>
            </el-menu-item>
            <el-menu-item index="/admin/admin-password">
              <el-icon><Lock /></el-icon>
              <template #title>修改密码</template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>

      <!-- 内容区域 -->
      <el-main class="scrollable-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAdminStore } from '@/stores/admin'
import { ElMessage } from 'element-plus'
import {
  Fold,
  Expand,
  ArrowDown,
  DataAnalysis,
  Collection,
  Bell,
  UserFilled,
  User,
  Operation,
  Setting,
  Avatar,
  Lock
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const adminStore = useAdminStore()

// 菜单折叠状态
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// // 判断是否是超级管理员（id为1）
// const isSuperAdmin = computed(() => adminStore.adminInfo?.id === 1)

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/admin/admin-profile')
      break
    case 'changePassword':
      router.push('/admin/admin-password')
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 退出登录
const handleLogout = () => {
  adminStore.clearAdminInfo()
  router.push('/admin/login')
  ElMessage.success('已退出登录')
}
</script>

<style scoped>
.admin-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-header {
  flex-shrink: 0;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.fold-btn {
  font-size: 24px;
  cursor: pointer;
  padding: 10px;
  border-radius: 4px;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
}

.fold-btn:hover {
  background-color: rgba(0, 0, 0, 0.08);
}

.header-title {
  font-size: 16px;
  font-weight: 500;
}

.header-right {
  display: flex;
  gap: 20px;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid #e0e0e0;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
  color: #409EFF;
  font-size: 14px;
  line-height: 1;
}

.el-dropdown-link .el-icon {
  margin-left: 5px;
}

.el-container {
  flex: 1;
  display: flex;
  min-height: 0;
}

.aside-menu {
  flex-shrink: 0;
  transition: width 0.3s;
  border-right: 1px solid #ddd;
  overflow: hidden;
}

.admin-menu {
  height: 100%;
  border-right: none;
}

.admin-menu:not(.el-menu--collapse) {
  width: 180px;
}

:deep(.el-menu--collapse) .el-sub-menu__title span,
:deep(.el-menu--collapse) .el-menu-item span {
  height: 0;
  width: 0;
  overflow: hidden;
  visibility: hidden;
  display: inline-block;
}

:deep(.el-menu--collapse) .el-sub-menu__icon-arrow {
  display: none;
}

.el-main.scrollable-main {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background-color: #fff;
}
</style>
